<?php

namespace Parse;

// Follows rules for scheme's cond expression
class CondParser extends Parser
{
    public function __construct($parsers, $callback = null)
    {
        if (count($parsers) === 0) {
            throw new ParseException("must have at least 1 parser");
        }
        $this->parsers = $parsers;
        parent::__construct($callback);
    }
    public function defaultCallback()
    {
        return func_get_arg(0);
    }

    public function matchProcedure($string)
    {
        foreach ($this->parsers as $parser) {
            try {
                $result = $parser->match($string);
                return array(
                    "matches" => array($result["match"]),
                    "rest" => $result["rest"]);
            } catch (ParseException $e) {
                continue;
            }
        }
        throw new ParseException("CondParser couldn't find a match");
    }
}
